﻿using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Eldert.IoT.Windows.Administration
{
    /// <summary>
    /// Class used to redirect console output to a textbox.
    /// </summary>
    class ConsoleRedirect : TextWriter
    {
        private readonly TextBox _textbox;

        public ConsoleRedirect(TextBox textbox)
        {
            _textbox = textbox;
        }

        public override void Write(char value)
        {
            if (_textbox.InvokeRequired)
            {
                _textbox.BeginInvoke((MethodInvoker)delegate { _textbox.AppendText(value.ToString()); });
            }
            else
            {
                _textbox.AppendText(value.ToString());
            }
        }

        public override Encoding Encoding => Encoding.ASCII;
    }
}